/**
 * Copyright (c) 2020-2021 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 *
 */
/**
 * @file    semphr.h
 * @brief   semphr funtion header file for freertos adapter
 * @details semphr funtion for freertos adapter
 * @version 1.0
 * @author  Hu lei
 * @date    2021-03-05
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2021-03-05               Hu lei              Create it.
 *
 */
#ifndef SEMAPHORE_H
#define SEMAPHORE_H

#ifndef INC_FREERTOS_H
    #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
#endif

#include "queue.h"

typedef QueueHandle_t SemaphoreHandle_t;

#define semBINARY_SEMAPHORE_QUEUE_LENGTH    ( ( uint8_t ) 1U )
#define semSEMAPHORE_QUEUE_ITEM_LENGTH      ( ( uint8_t ) 0U )
#define semGIVE_BLOCK_TIME                  ( ( TickType_t ) 0U )

#define xQueueReceive               turboxRecvFromQueue			
#define uxQueueMessagesWaiting      turboxWaitQueueMsg			
#define vQueueDelete                turboxDeleteQueue				
#define xQueueGenericSend           turboxSendQueue			
#define xQueueGenericSendFromISR    turboxSendQueueFromISR			

QueueHandle_t turboxCreateDynamicMux(void);
BaseType_t turboxPutSem(QueueHandle_t sem);
void turboxDelSemMux(QueueHandle_t xQueue); 
QueueHandle_t turboxCreateRecursiveMux();
QueueHandle_t turboxSemaphoreCreateBinary(void);
BaseType_t turboxPutSemRecursive(QueueHandle_t xMutex);
QueueHandle_t turboxCreateDynamicBinarySem(SemaphoreHandle_t sem);
QueueHandle_t turboxCreateStaticMux(StaticQueue_t *pxStaticQueue );
QueueHandle_t turboxCreateStaticBinarySem(StaticQueue_t *pxStaticQueue);
BaseType_t turboxGetSem(QueueHandle_t xQueue, TickType_t xTicksToWait);
BaseType_t turboxGetSemRecursive(QueueHandle_t xMutex, TickType_t xTicksToWait);
QueueHandle_t turboxGetSemCount(const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount );
BaseType_t turboxPutSemFromISR(QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken);

#define vSemaphoreCreateBinary( xSemaphore )                                                                                            \
    {                                                                                                                                   \
        ( xSemaphore ) = turboxCreateDynamicBinarySem( xSemaphore );                                                                         \
        if( ( xSemaphore ) != NULL )                                                                                                    \
        {                                                                                                                               \
            ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                                  \
        }                                                                                                                               \
    }
#define xSemaphoreCreateMutex() turboxCreateDynamicMux()
#define xSemaphoreCreateBinary() turboxSemaphoreCreateBinary() 
#define vSemaphoreDelete( xSemaphore ) turboxDelSemMux( xSemaphore )
#define xSemaphoreGive( xSemaphore )   turboxPutSem( xSemaphore )
#define xSemaphoreCreateRecursiveMutex() turboxCreateRecursiveMux() 
#define xSemaphoreGiveRecursive( xMutex )   turboxPutSemRecursive( xMutex )
#define xSemaphoreTake( xSemaphore, xBlockTime )  turboxGetSem( xSemaphore, xBlockTime )
#define xSemaphoreCreateMutexStatic( pxMutexBuffer ) turboxCreateStaticMux( pxMutexBuffer )
#define uxSemaphoreGetCount( xSemaphore ) turboxWaitQueueMsg( ( QueueHandle_t ) ( xSemaphore ) )
#define xSemaphoreTakeRecursive( xMutex, xBlockTime )   turboxGetSemRecursive( xMutex, xBlockTime )
#define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) turboxCreateStaticBinarySem( pxStaticSemaphore )
#define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) turboxGetSemCount( uxMaxCount, uxInitialCount )
#define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken )  turboxPutSemFromISR( xSemaphore, pxHigherPriorityTaskWoken )
#endif /* SEMAPHORE_H */


